package com.lzk.trigger.job;

import com.alibaba.fastjson.JSON;
import com.lzk.domain.IDynamicThreadPoolService;
import com.lzk.domain.model.ThreadPoolConfigEntity;
import com.lzk.registry.IRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @description: 线程池数据上报任务
 * @date 2025/9/10 9:46
 */
public class ThreadPoolDataReportJob {

    private Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);

    private final IDynamicThreadPoolService dynamicThreadPoolService;

    private final IRegistry registry;


    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }


    @Scheduled(cron = "0/20 * * * * ?")
    public void execute() {

        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);
        logger.info("开始执行线程池数据上报任务:{}", JSON.toJSONString(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolConfig(threadPoolConfigEntity);
            logger.info("上报线程池配置:{}", JSON.toJSONString(threadPoolConfigEntity));
        }


    }
}
